package com.yanze.schoolinformation.view;

import android.databinding.DataBindingUtil;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;

import com.yanze.schoolinformation.R;

import java.util.ArrayList;
import java.util.List;

import com.yanze.schoolinformation.databinding.ActivityHomeBinding;
import com.yanze.schoolinformation.utils.CommonUtils;
import com.yanze.schoolinformation.utils.statusbar.StatusBarUtil;
import com.yanze.schoolinformation.view.community.CommunityFragment;
import com.yanze.schoolinformation.view.home.*;
import com.yanze.schoolinformation.view.information.InformationFragment;
import com.yanze.schoolinformation.view.my.MyFragment;

public class HomeActivity extends AppCompatActivity {

    private List<Fragment> mFragments;
    private ActivityHomeBinding databinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        databinding = DataBindingUtil.setContentView(this, R.layout.activity_home);
//        StatusBarUtil.setColor(this, CommonUtils.getColor(R.color.colorTheme));//设置状态栏透明
        StatusBarUtil.setColorNoTranslucent(this, CommonUtils.getColor(R.color.colorTheme));//设置状态栏透明
        initView();
    }
    protected boolean isBinding() {
        return false;
    }

    public void initView() {
        mFragments = new ArrayList<>();
        mFragments.add(new HomeFragment());
        mFragments.add(new InformationFragment());
        mFragments.add(new CommunityFragment());
        mFragments.add(new MyFragment());

        MyAdapter mAdapter = new MyAdapter(getSupportFragmentManager());
        databinding.homeVp.setAdapter(mAdapter);
        databinding.homeVp.setOffscreenPageLimit(4);
        databinding.homeBbl.setViewPager(databinding.homeVp);
        databinding.homeBbl.setSmoothScroll(true);//开启ViewPager切换动画

//        databinding.homeBbl.setUnread(0,20);//设置第一个页签的未读数为20
//        databinding.homeBbl.setUnread(1,101);//设置第二个页签的未读数
//        databinding.homeBbl.showNotify(2);//设置第三个页签显示提示的小红点
//        databinding.homeBbl.setMsg(3,"NEW");//设置第四个页签显示NEW提示文字
//        databinding.homeBbl.hideNotify(2);//隐藏第三个页签显示提示的小红点
//        databinding.homeBbl.hideMsg(3);//隐藏第四个页签显示的提示文字
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
}
